package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.miaodong.entity.AgentUserRecords;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
public class AgentUserRecordsDao extends BaseDao<AgentUserRecords> {

    public AgentUserRecords exist(Integer userId) {
        String sql = "select * from agent_user_records where userId = "+ userId;
        return this.queryObj(sql, new ArrayList<>(), AgentUserRecords.class);
    }

    public AgentUserRecords findByParent(Integer userId) {
        String sql = "select * from agent_user_records where userId = "+ userId;
        return this.queryObj(sql, new ArrayList<>(), AgentUserRecords.class);
    }

    public String subUserId(Integer parentUserId) {
        String sql = "select * from agent_user_records where parentUserId = "+ parentUserId;
        List<AgentUserRecords> agentUserRecords = this.queryList(sql, new ArrayList<>(), AgentUserRecords.class);

        return agentUserRecords.stream().map(e -> e.getUserId().toString()).collect(Collectors.joining(","));
    }

    public List<AgentUserRecords> list(Integer parentUserId) {
        String sql = "select r.*,u.sex from agent_user_records r " +
                " left join app_user u " +
                " on r.userId = u.id" +
                " where r.parentUserId = "+ parentUserId;
        return this.queryList(sql, new ArrayList<>(), AgentUserRecords.class);
    }

    public List<AgentUserRecords> todayNewUser(Integer parentUserId) {
        String sql = "select r.*,u.sex from agent_user_records r " +
                " left join app_user u " +
                " on r.userId = u.id" +
                " where r.parentUserId = "+ parentUserId +
                " and to_days(r.createTime) = to_days(now())";
        return this.queryList(sql, new ArrayList<>(), AgentUserRecords.class);
    }

    public Map<String, Object> rechargeToday(String sonUserIds) {
        String sql = "SELECT IFNULL(sum(money),0) as totalMoneyToday FROM app_order " +
                " where status=2 and state=2 " +
                " and userid in ("+ sonUserIds +") " +
                " and TO_DAYS(chongzhitime) = TO_DAYS(now())";
        return this.queryObj(sql, new ArrayList<>(), Map.class);
    }

    public Map<String, Object> withdrawToday(String sonUserIds) {
        return new HashMap<>();
    }
}
